export default{
	
	getUser(){
		let user = uni.getStorageSync('user');
		if(user != undefined && Object.keys(user).length > 0){
			return user;
		}else{
			uni.showToast({
				title: '未登录,请登录',
				icon: 'none',
				duration: 2000,
				success(res) {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}
			})
			return {id: 0};
		}
	},
	
	/**
	 * @description: 将小程序api封装成Promise
	 * @param {any} func js api，例如tt.login
	 * @param {*} options 传入api的参数
	 * @return {*}
	 */
	promisify(func, options = {}) {
	  return new Promise((resolve, reject) => {
	    func({
	      ...options,
	      success(res) {
	        resolve(res);
	      },
	      fail(err) {
	        reject(err);
	      },
	    });
	  });
	},
	
	generateRandomString(length) {
	    var result           = '';
	    var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	    var charactersLength = characters.length;
	    for (var i = 0; i < length; i++) {
	        result += characters.charAt(Math.floor(Math.random() * charactersLength));
	    }
	    return result;
	},
	
	//数组对象排序
	createComprisonFunction(propertyName){
	    return function(object1,object2){
	        var value1 = object1[propertyName];
	        var value2 = object2[propertyName];
	        if(value1 < value2){
	            return -1;
	        }else if(value1 > value2){
	            return 1;
	        }else{
	            return 0;
	        }
	    }
	},
	
	//隐藏手机号中间四位
	hidePhone(phone) {
		if(phone){
			let reg = /^(\d{3})\d{4}(\d{4})$/;
			return phone.replace(reg, '$1****$2');
		}else{
			return ''
		}
	},
	
	strToArr(str){
		if(str){
			str = str.replace(new RegExp("，","gm",";","；",":","、"),",");
			let arr = str.split(',');
			return arr;
		}
		return [];
	},
	
	//判断对象是否有值
	objectValueAllEmpty (object) {
	    var isEmpty = true;
	    Object.keys(object).forEach(function(x) {
	        if(object[x] != null && object[x] instanceof Array && object[x].length > 0){
	            object[x].forEach((item)=>{
	                isEmpty = objectValueAllEmpty(item);
	            })
	        }else if(object[x] != null && object[x] != ''){
	            isEmpty = false;
	        } 
	    });
	    return isEmpty;
	},
	
	/*封装 四则运算*/
	/**
	 ** 加法函数，用来得到精确的加法结果
	 ** 说明：javascript的加法结果会有误差，在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
	 ** 调用：accAdd(arg1,arg2)
	 ** 返回值：arg1加上arg2的精确结果
	 **/
	addNum(arg1, arg2) {
		if(!arg1){
			arg1 = 0;
		}
		if(!arg2){
			arg2 = 0;
		}
	  var r1, r2, m, c;
	  try {
	    r1 = arg1.toString().split(".")[1].length;
	  } catch (e) {
	    r1 = 0;
	  }
	  try {
	    r2 = arg2.toString().split(".")[1].length;
	  } catch (e) {
	    r2 = 0;
	  }
	  c = Math.abs(r1 - r2);
	  m = Math.pow(10, Math.max(r1, r2));
	  if (c > 0) {
	    var cm = Math.pow(10, c);
	    if (r1 > r2) {
	      arg1 = Number(arg1.toString().replace(".", ""));
	      arg2 = Number(arg2.toString().replace(".", "")) * cm;
	    } else {
	      arg1 = Number(arg1.toString().replace(".", "")) * cm;
	      arg2 = Number(arg2.toString().replace(".", ""));
	    }
	  } else {
	    arg1 = Number(arg1.toString().replace(".", ""));
	    arg2 = Number(arg2.toString().replace(".", ""));
	  }
	  return (arg1 + arg2) / m;
	},
	
	/**
	 ** 减法函数，用来得到精确的减法结果
	 ** 说明：javascript的减法结果会有误差，在两个浮点数相减的时候会比较明显。这个函数返回较为精确的减法结果。
	 ** 调用：accSub(arg1,arg2)
	 ** 返回值：arg1加上arg2的精确结果
	 **/
	subNum(arg1, arg2) {
		if(!arg1){
			arg1 = 0;
		}
		if(!arg2){
			arg2 = 0;
		}
	  var r1, r2, m, n;
	  try {
	    r1 = arg1.toString().split(".")[1].length;
	  } catch (e) {
	    r1 = 0;
	  }
	  try {
	    r2 = arg2.toString().split(".")[1].length;
	  } catch (e) {
	    r2 = 0;
	  }
	  m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
	  n = (r1 >= r2) ? r1 : r2;
	  return ((arg1 * m - arg2 * m) / m).toFixed(n);
	},
	 
	/**
	 ** 乘法函数，用来得到精确的乘法结果
	 ** 说明：javascript的乘法结果会有误差，在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
	 ** 调用：accMul(arg1,arg2)
	 ** 返回值：arg1乘以 arg2的精确结果
	 **/
	mulNum(arg1, arg2) {
		if(!arg1){
			arg1 = 0;
		}
		if(!arg2){
			arg2 = 0;
		}
	  var m = 0,
	    s1 = arg1.toString(),
	    s2 = arg2.toString();
	  try {
	    m += s1.split(".")[1].length;
	  } catch (e) {}
	  try {
	    m += s2.split(".")[1].length;
	  } catch (e) {}
	  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
	},
	 
	
	/** 
	 ** 除法函数，用来得到精确的除法结果
	 ** 说明：javascript的除法结果会有误差，在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
	 ** 调用：accDiv(arg1,arg2)
	 ** 返回值：arg1除以arg2的精确结果
	 **/
	divNum(arg1, arg2) {
		console.log(arg1)
		console.log(arg2)
	  var t1 = 0,
	    t2 = 0,
	    r1, r2;
	  try {
	    t1 = arg1.toString().split(".")[1].length;
	  } catch (e) {}
	  try {
	    t2 = arg2.toString().split(".")[1].length;
	  } catch (e) {}
	  r1 = Number(arg1.toString().replace(".", ""));
	  r2 = Number(arg2.toString().replace(".", ""));
	  return (r1 / r2) * Math.pow(10, t2 - t1);
	},
	
	/**
	 * 构造树型结构数据
	 * @param {*} data 数据源
	 * @param {*} id id字段 默认 'id'
	 * @param {*} parentId 父节点字段 默认 'parentId'
	 * @param {*} children 孩子节点字段 默认 'children'
	 */
	handleTree(data, id, parentId, children) {
	  let config = {
	    id: id || 'id',
	    parentId: parentId || 'parentId',
	    childrenList: children || 'children'
	  };
	
	  var childrenListMap = {};
	  var nodeIds = {};
	  var tree = [];
	
	  for (let d of data) {
	    let parentId = d[config.parentId];
	    if (childrenListMap[parentId] == null) {
	      childrenListMap[parentId] = [];
	    }
	    nodeIds[d[config.id]] = d;
	    childrenListMap[parentId].push(d);
	  }
	
	  for (let d of data) {
	    let parentId = d[config.parentId];
	    if (nodeIds[parentId] == null) {
	      tree.push(d);
	    }
	  }
	
	  for (let t of tree) {
	    adaptToChildrenList(t);
	  }
	
	  function adaptToChildrenList(o) {
	    if (childrenListMap[o[config.id]] !== null) {
	      o[config.childrenList] = childrenListMap[o[config.id]];
	    }
	    if (o[config.childrenList]) {
	      for (let c of o[config.childrenList]) {
	        adaptToChildrenList(c);
	      }
	    }
	  }
	  return tree;
	}
}